package com.rem.hutool.my4IO;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;

import java.io.*;

/**
 * IO工具类
 * 针对InputStream、OutputStream、Reader、Writer封装简化
 * 对NIO相关操作做封装简化
 *
 * @author Rem
 * @date 2020-08-18
 */

public class IoUtilDemo {

    public static void main(String[] args) throws Exception {


        //out();

        //utilRead();

        //copyFile();

        //jdkRead();

    }

    /**
     * 文件写出
     * @throws IOException
     */
    private static void out() throws IOException {
        BufferedOutputStream out = FileUtil.getOutputStream("../../src/main/resources/static/name3.txt");
        OutputStreamWriter outer = IoUtil.getUtf8Writer(out);
        outer.write("rem is my name !!!");
        outer.flush();
        outer.close();
    }



    /**
     * 文件拷贝
     */
    private static void copyFile() {
        BufferedInputStream in = FileUtil.getInputStream("classpath:static/name.txt");
        BufferedOutputStream out = FileUtil.getOutputStream("../../src/main/resources/static/name2.txt");
        long copySize = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE);
        System.err.println(copySize);
    }

    /**
     * util读出数据
     * @throws IOException
     */
    private static void utilRead() throws IOException {
        BufferedInputStream in = FileUtil.getInputStream("classpath:static/name.txt");
        BufferedReader reader = IoUtil.getUtf8Reader(in);
        System.err.println(reader.readLine());
    }

    /**
     * jdk原生读出数据
     *
     * @throws IOException
     */
    private static void jdkRead() throws IOException {
        BufferedInputStream in = FileUtil.getInputStream("classpath:static/name.txt");
        int size;
        byte[] bytes = new byte[1024];
        while ((size = in.read(bytes)) != -1) {
            System.err.println(new String(bytes, 0, size));
        }
    }


}
